package com.jingbit.ai.helper;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pansha on 2017/7/3.
 */

public class PermissionHelper {
    private static final String TAG="PermissionHelper";


    //Android 6.0 需要请求权限
    @TargetApi(23)
    public static void requestNeedPermissions(Activity activity) {
        Logger.log(TAG,"requestNeedPermissions", Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= 23) {
            List<String> permissionsNeeded = new ArrayList<String>();
            final List<String> permissionsList = new ArrayList<String>();
            if (!addPermission(activity, permissionsList, Manifest.permission.CAMERA))
                permissionsNeeded.add("获取相机权限");
            if (!addPermission(activity, permissionsList, Manifest.permission.READ_PHONE_STATE))
                permissionsNeeded.add("获取手机信息");
            if (!addPermission(activity, permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
                permissionsNeeded.add("写入SD数据卡");
            if (!addPermission(activity, permissionsList, Manifest.permission.READ_CONTACTS))
                permissionsNeeded.add("读取通话录");
            if (!addPermission(activity, permissionsList, Manifest.permission.READ_CALL_LOG))
                permissionsNeeded.add("读取通话记录");
            if (!addPermission(activity, permissionsList, Manifest.permission.READ_SMS))
                permissionsNeeded.add("读取短信记录");
            if (!addPermission(activity, permissionsList,Manifest.permission.ACCESS_FINE_LOCATION))
                permissionsNeeded.add("定位 ACCESS_FINE_LOCATION");
            if (!addPermission(activity, permissionsList,Manifest.permission.ACCESS_COARSE_LOCATION))
                permissionsNeeded.add("定位 ACCESS_COARSE_LOCATION");
            if (!addPermission(activity, permissionsList,Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS))
                permissionsNeeded.add("定位 ACCESS_LOCATION_EXTRA_COMMANDS");
            if (!addPermission(activity, permissionsList,Manifest.permission.SYSTEM_ALERT_WINDOW)) {
                permissionsNeeded.add("弹框 SYSTEM_ALERT_WINDOW");
            }
            Logger.log(TAG,"未开启权限有",permissionsNeeded.toString());
            if (permissionsList.size() > 0) {
                activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), 100);
                return;
            }

        }
    }

    @TargetApi(23)
    private static boolean addPermission(Activity activity, List<String> permissionsList,
                                  String permission) {
        Logger.log(TAG,"addPermission",permission);
        if (activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (activity.shouldShowRequestPermissionRationale(permission))
                return false;
        }
        Logger.log(activity,"addPermission",true);
        return true;
    }
}
